#include "view_sports.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "Motor.h"
#include "view.h"
#include "times/view_times.h"
#include "sports_mode.h"
#include "app_trace.h"
#include "view_sports.inc"
#include "times/view_time_0.h"
#include "localize.h"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)
void view_sports_base_display(void)
{
#ifdef TEST_UI
    disp_ctrl.auto_refresh_time = 1000;
    disp_ctrl.auto_close_delay = 2000;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 0, 0, FILE_IMG_view_2);
#else
    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
    }
    if (disp_ctrl.com_data.i_temp[5])
    {
        disp_draw_user_image(42, 3, img_ble_connect);
    }
    else
    {
        disp_draw_user_image(42, 0, img_ble_disconnect);
    }
    show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 90, 200, FILE_IMG_S105_sport_text_90_200);
    // disp_show_localize(DISP_LFS_FILE_INDEX_0, localize_string_key_Sport, FILE_IMG_localize_en_Sport_90_200, 90, 200, 50);
    disp_show_localize(DISP_LFS_FILE_INDEX_0, localize_string_key_Sport, 50);
#if defined(Z2_BOARD) || defined(BT100_BOARD)
#if defined(SW07_PROJECT)
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 48, 24, FILE_IMG_fdg);
#else
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 47, 24, FILE_IMG_1001_sport_47_24);
#endif
#else
    img_gif_com_t const img_cfg_2 = {
        .gif_count = 4,
        .img[0].x = 45,
        .img[0].y = 17,
        .img[0].sn = FILE_IMG_S101_sport_1_45_17,
        .img[1].x = 45,
        .img[1].y = 17,
        .img[1].sn = FILE_IMG_S102_sport_2_45_17,
        .img[2].x = 45,
        .img[2].y = 17,
        .img[2].sn = FILE_IMG_S103_sport_3_45_17,
        .img[3].x = 45,
        .img[3].y = 17,
        .img[3].sn = FILE_IMG_S104_sport_4_45_17,
    };
    _disp_show_flash_img_gif_all(&img_cfg_2, DISP_LFS_FILE_INDEX_1);
#endif
#endif
}

view_event_result_t view_sports_level_1_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
    {
        if (view == view_sports_level_1_begin)
        {
            display_show(view_sports_level_1_end - 1, DISP_MODE_UP_MOVE);
            return view_event_result_handled;
        }
        return view_event_result_not_handled;
    }
#endif //!< EVENT_PREVIOUS_VIEW
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
    {
        if (view == view_sports_level_1_end - 1)
        {
            display_show(view_sports_level_1_begin, DISP_MODE_DOWN_MOVE);
            return view_event_result_handled;
        }
        return view_event_result_not_handled;
    }
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
    {
        display_show(view_sports_base, DISP_MODE_UP_MOVE);
        return view_event_result_handled;
    }
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
    {
        sports_mode_mode_t mode = sports_mode_mode_invalid;
        switch (view)
        {
        case view_sports_runing:
            mode = sports_mode_mode_run_2;
            break;
        case view_sports_walking:
            mode = sports_mode_mode_walk;
            break;
        case view_sports_cycling:
            mode = sports_mode_mode_ride;
            break;
        case view_sports_fitness:
            mode = sports_mode_mode_work_out;
            break;
        case view_sports_swing:
            mode = sports_mode_mode_swing;
            break;
        default:
            break;
        }
        sports_mode_start(mode);
        display_show(view_sports_state_0, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    }
    return view_event_result_not_handled;
}
#define POS_WIDTH 6
#define POS_INTERVAL 7
void view_sports_level_1_display(int view)
{

    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
        disp_ctrl.auto_refresh_time = 500;
    }
    if (disp_ctrl.com_data.i_temp[5])
    {
        disp_draw_user_image(42, 3, img_ble_connect);
    }
    else
    {
        disp_draw_user_image(42, 0, img_ble_disconnect);
    }
    show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);
    switch (view)
    {
    case view_sports_runing:
#if defined(Z2_BOARD) || defined(BT100_BOARD)
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 47, 24, FILE_IMG_1101_run_47_24);
#else
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 44, 38, FILE_IMG_S201_run_44_38);
#endif

        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 100, 189, FILE_IMG_S207_run_text_100_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportRun, FILE_IMG_localize_en_SportRun_100_189, 100, 189, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportRun, 50);
        break;
    case view_sports_walking:
#if defined(Z2_BOARD) || defined(BT100_BOARD)
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 47, 24, FILE_IMG_1102_walk_47_24);
#else
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 80, 34, FILE_IMG_S202_walk_80_34);
#endif

        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 91, 189, FILE_IMG_S208_walk_text_91_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportWalk, FILE_IMG_localize_en_SportWalk_91_189, 91, 189, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportWalk, 50);
        break;
    case view_sports_cycling:
#if defined(Z2_BOARD) || defined(BT100_BOARD)
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 47, 24, FILE_IMG_1103_cycle_47_24);
#else
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 48, 39, FILE_IMG_S203_cycling_48_39);
#endif

        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 78, 189, FILE_IMG_S209_cycling_text_78_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportCycling, FILE_IMG_localize_en_SportCycling_78_189, 78, 189, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportCycling, 50);
        break;
    case view_sports_fitness:
#if defined(Z2_BOARD) || defined(BT100_BOARD)
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 47, 24, FILE_IMG_1105_treadmill_47_24);
#else
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 69, 31, FILE_IMG_S205_fitness_69_31);
#endif

        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 80, 189, FILE_IMG_S211_fitness_text_80_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportFitness, FILE_IMG_localize_en_SportFitness_80_189, 80, 189, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportFitness, 50);
        break;
    case view_sports_swing:
#if defined(Z2_BOARD) || defined(BT100_BOARD)
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 47, 24, FILE_IMG_1104_swing_47_24);
#else
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 45, 35, FILE_IMG_S206_swing_45_35);
#endif
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 87, 189, FILE_IMG_S212_swing_text_87_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportSwing, FILE_IMG_localize_en_SportSwing_87_189, 87, 189, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportSwing, 50);
        break;
    default:
        return;
    }
}
static bool is_level_1_view(int view)
{
    if (view >= view_sports_level_1_begin && view < view_sports_level_1_end)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void view_sports_level_1_back_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
        disp_ctrl.auto_refresh_time = 500;
    }
    if (disp_ctrl.com_data.i_temp[5])
    {
        disp_draw_user_image(42, 3, img_ble_connect);
    }
    else
    {
        disp_draw_user_image(42, 0, img_ble_disconnect);
    }
    show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 53, 30, FILE_IMG_E103_back_53_30);
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 93, 188, FILE_IMG_E106_back_text_93_188);
    // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_Back, FILE_IMG_localize_en_Back_93_188, 93, 188, 50);
    disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_Back, 50);
}
void view_sports_prompt_end_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 3000;
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
    }
    if (disp_ctrl.auto_refresh_count == 0)
    {
        show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
        show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 69, 40, FILE_IMG_S517_end_icon_69_40);
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 34, 183, FILE_IMG_S516_end_text_34_183);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_AlertSportEnd, FILE_IMG_localize_en_AlertSportEnd_34_183, 34, 183, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_AlertSportEnd, 50);
    }
    else
    {
        if (disp_ctrl.loop_cnt == 0)
        {
            display_show(view_sports_result_0, DISP_MODE_NORMAL);
        }
    }
}
void view_sports_error_short_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 3000;
    }
    if (disp_ctrl.auto_refresh_count == 0)
    {
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 26, 58, FILE_IMG_S514_notsave_26_58);
        // disp_show_localize(DISP_LFS_FILE_INDEX_0, localize_string_key_AlertSportNotSave, FILE_IMG_localize_en_AlertSportNotSave_26_58, 26, 58, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_0, localize_string_key_AlertSportNotSave, 50);
    }
    else
    {
        if (disp_ctrl.loop_cnt == 0)
        {
            display_show(view_sports, DISP_MODE_NORMAL);
        }
    }
}
static void show_sport_mode_text(int mode)
{
    switch (mode)
    {
    case sports_mode_mode_run_2:
    {
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 67, 17, FILE_IMG_S207_run_text_100_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateRun, FILE_IMG_localize_en_SportRun_100_189, 67, 17, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateRun, 50);
    }
    break;
    case sports_mode_mode_ride:
    {
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 67, 17, FILE_IMG_S209_cycling_text_78_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateCycling, FILE_IMG_localize_en_SportCycling_78_189, 67, 17, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateCycling, 50);
    }

    break;
    case sports_mode_mode_work_out:
    {
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 67, 17, FILE_IMG_S211_fitness_text_80_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateFitness, FILE_IMG_localize_en_SportFitness_80_189, 67, 17, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateFitness, 50);
    }

    break;
    case sports_mode_mode_walk:
    {
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 67, 17, FILE_IMG_S208_walk_text_91_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateWalk, FILE_IMG_localize_en_SportWalk_91_189, 67, 17, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateWalk, 50);
    }

    break;
    case sports_mode_mode_swing:
    {
        //_disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 67, 17, FILE_IMG_S212_swing_text_87_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateSwing, FILE_IMG_localize_en_SportSwing_87_189, 67, 17, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateSwing, 50);

        break;
    }
    }
}

void show_sport_mode_icon(int mode)
{
    switch (mode)
    {
    case sports_mode_mode_run_2:
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 3, 3, FILE_IMG_S401_runsmall_3_3);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 14, 79, FILE_IMG_time_distance_heart_rate_14_79);
    }
    break;
    case sports_mode_mode_ride:
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 3, 3, FILE_IMG_S403_cyclingsmall_3_3);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 14, 79, FILE_IMG_time_calorie_heart_rate_14_79);
    }
    break;
    case sports_mode_mode_work_out:
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 3, 3, FILE_IMG_S405_fienesssmall_3_3);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 14, 79, FILE_IMG_time_calorie_heart_rate_14_79);
    }
    break;
    case sports_mode_mode_walk:
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 3, 3, FILE_IMG_S402_walksmall_3_3);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 14, 79, FILE_IMG_time_distance_heart_rate_14_79);
    }
    break;
    case sports_mode_mode_swing:
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 3, 3, FILE_IMG_S406_swingsmall_3_3);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 14, 79, FILE_IMG_time_calorie_heart_rate_14_79);
    }
    break;
    }
}
static void show_back_line(void)
{
    //! 0x39c7
    disp_draw_rect_color(0, 62, 239, 62, 0x39c7);
    disp_draw_rect_color(0, 120, 239, 120, 0x39c7);
    disp_draw_rect_color(0, 177, 239, 177, 0x39c7);
}
void show_start_time(uint32_t time_stamp)
{
    datetime_t time = utc_time_to_date_time(time_stamp);
    int x = 60;
    int const y = 19;
    int const year = time.date.year % 100;
    disp_draw_user_image(x, y, img_j101_TAB[year / 10]);
    x += 9;
    disp_draw_user_image(x, y, img_j101_TAB[year % 10]);
    x += 9;
    disp_draw_user_image(x, y, img_j101_under_line);
    x += 7;
    int const month = time.date.month % 100;
    disp_draw_user_image(x, y, img_j101_TAB[month / 10]);
    x += 9;
    disp_draw_user_image(x, y, img_j101_TAB[month % 10]);
    x += 9;
    disp_draw_user_image(x, y, img_j101_under_line);
    x += 7;
    int const day = time.date.day % 100;
    disp_draw_user_image(x, y, img_j101_TAB[day / 10]);
    x += 9;
    disp_draw_user_image(x, y, img_j101_TAB[day % 10]);
    x += 9;
    x += 8;

    int hour = time.time.hour % 100;
    disp_draw_user_image(x, y, img_j101_TAB[hour / 10]);
    x += 9;
    disp_draw_user_image(x, y, img_j101_TAB[hour % 10]);
    x += 9;
    disp_draw_user_image(x, y, img_j101_two_point);
    x += 5;

    int minute = time.time.minute % 100;
    disp_draw_user_image(x, y, img_j101_TAB[minute / 10]);
    x += 9;
    disp_draw_user_image(x, y, img_j101_TAB[minute % 10]);
    x += 9;
}

void view_sports_result_0_display(void)
{

    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = sports_mode_get_now_mode();
        disp_ctrl.com_data.i_temp[2] = sports_mode_get_sports_duration(); //时间
        disp_ctrl.com_data.i_temp[3] = sports_mode_get_sports_minimal_heart_rate();
        disp_ctrl.com_data.i_temp[4] = sports_mode_get_sports_maximum_heart_rate();
        disp_ctrl.com_data.i_temp[5] = sports_mode_get_sports_calorie() / 1000;
        disp_ctrl.com_data.i_temp[6] = sports_mode_get_sports_start_time();
        if (system_config_get_km_mi() != 0)
        {
            disp_ctrl.com_data.i_temp[7] = sports_mode_get_sports_mileage() * 0.6213712;
            disp_ctrl.com_data.i_temp[8] = 0;
        }
        else
        {
            disp_ctrl.com_data.i_temp[7] = sports_mode_get_sports_mileage();
            disp_ctrl.com_data.i_temp[8] = 1;
        }
    }
    show_sport_mode_icon(disp_ctrl.com_data.i_temp[0]);
    show_back_line();
    {
        switch (disp_ctrl.com_data.i_temp[0])
        {
        case sports_mode_mode_ride:
        case sports_mode_mode_work_out:
        case sports_mode_mode_swing:
        {
            int x_end = disp_draw_user_image_num(60, 137, disp_ctrl.com_data.i_temp[5], 14 + 3, img_j401_TAB);
            disp_draw_user_image(x_end, 137 + 12, img_common_kcal);
            break;
        }
        case sports_mode_mode_run_2:
        case sports_mode_mode_walk:
        {
            int x = disp_draw_user_image_num(60, 137, disp_ctrl.com_data.i_temp[7] / 1000, 14 + 3, img_j401_TAB);
            disp_draw_user_image(x, 137, img_j401_point);
            x += 5 + 3;
            x = disp_draw_user_image_num_with_digits(x, 137, disp_ctrl.com_data.i_temp[7] % 1000 / 10, 2, 14 + 3, img_j401_TAB);
            if (disp_ctrl.com_data.i_temp[8])
            {
                disp_draw_user_image(x + 4, 137 + 15, img_common_km);
            }
            else
            {
                disp_draw_user_image(x + 4, 137 + 15, img_common_mi);
            }
            break;
        }
        }
    }
    {
        int duration = disp_ctrl.com_data.i_temp[2];
        int second = duration % 60;
        duration /= 60;
        int minute = duration % 60;
        duration /= 60;
        int hour = duration % 60;
        int x = 60;
        int const y = 75;
        int const font_width = 14;
        int const point_width = 6;
        int const interval = 3;
        disp_draw_user_image(x, y, img_j401_TAB[hour / 10]);
        x += font_width + interval;
        disp_draw_user_image(x, y, img_j401_TAB[hour % 10]);
        x += font_width + interval;
        disp_draw_user_image(x, y, img_j401_two_point);
        x += point_width + interval;
        disp_draw_user_image(x, y, img_j401_TAB[minute / 10]);
        x += font_width + interval;
        disp_draw_user_image(x, y, img_j401_TAB[minute % 10]);
        x += font_width + interval;
        disp_draw_user_image(x, y, img_j401_two_point);
        x += point_width + interval;
        disp_draw_user_image(x, y, img_j401_TAB[second / 10]);
        x += font_width + interval;
        disp_draw_user_image(x, y, img_j401_TAB[second % 10]);
    }
    {
        int min_heart_rate = disp_ctrl.com_data.i_temp[3];
        int max_heart_rate = disp_ctrl.com_data.i_temp[4];
        if (min_heart_rate < 40)
        {
            min_heart_rate = 0;
        }
        else if (min_heart_rate > 220)
        {
            min_heart_rate = 220;
        }
        if (max_heart_rate < 40)
        {
            max_heart_rate = 0;
        }
        else if (max_heart_rate > 220)
        {
            max_heart_rate = 220;
        }
        if (min_heart_rate > max_heart_rate)
        {
            min_heart_rate = max_heart_rate;
        }
        int x = 60;
        int const y = 193;
        if (min_heart_rate == 0 || max_heart_rate == 0)
        {
            int const num_width = 14;
            int const num_interval = 3;
            int const num_height = 30;
            int x1 = x;
            int y1 = y + num_height / 2 - 2;
            int x2 = x1 + num_width - 1;
            int y2 = y1 + 3;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
            x1 += num_width + num_interval;
            x2 = x1 + num_width - 1;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
        }
        else
        {
            disp_draw_user_image(x, y, img_j401_TAB[min_heart_rate / 100]);
            x += 17;
            min_heart_rate %= 100;
            disp_draw_user_image(x, y, img_j401_TAB[min_heart_rate / 10]);
            x += 17;
            min_heart_rate %= 10;
            disp_draw_user_image(x, y, img_j401_TAB[min_heart_rate]);
            x += 17;
            disp_draw_rect_color(x, y + 15 - 2, x + 14, y + 15 + 1, RGB565_COLOR_WHITE);
            x += 17;
            disp_draw_user_image(x, y, img_j401_TAB[max_heart_rate / 100]);
            x += 17;
            max_heart_rate %= 100;
            disp_draw_user_image(x, y, img_j401_TAB[max_heart_rate / 10]);
            x += 17;
            max_heart_rate %= 10;
            disp_draw_user_image(x, y, img_j401_TAB[max_heart_rate]);
            x += 17;
            disp_draw_user_image(x, y + 14, img_common_bpm);
        }
    }
    show_start_time((uint32_t)disp_ctrl.com_data.i_temp[6]);
}

void view_sports_result_1_display(void)
{
    int const heart_50_height = 33;
    uint8_t *detail_buffer = (uint8_t *)&disp_ctrl.com_data.i_temp[5];
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = sports_mode_get_sports_avg_heart_rate();
        disp_ctrl.com_data.i_temp[1] = sports_mode_get_sports_minimal_heart_rate();
        disp_ctrl.com_data.i_temp[2] = sports_mode_get_sports_maximum_heart_rate();
        int const start_y = 59 + 126;
        disp_ctrl.com_data.i_temp[3] = sports_mode_get_detail_info((sports_mode_detail_t *)detail_buffer, SPORTS_MODE_DETAIL_CACHE_COUNT);
        // change to y
        for (int i = 0; i < disp_ctrl.com_data.i_temp[3]; i++)
        {
            if (detail_buffer[i] != 0)
            {
                detail_buffer[i] = start_y - (detail_buffer[i] - 50) * heart_50_height / 50;
            }
        }
    }

    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 42, 10, FILE_IMG_S511_heartrate_2_42_10);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 0, 59, FILE_IMG_S516_chart_0_59);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 50, 200, FILE_IMG_S512_highest_50_200);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, 138, 200, FILE_IMG_S513_lowest_138_200);

    int x = 0;
    int y = 0;
    if (disp_ctrl.com_data.i_temp[1] == 0 || disp_ctrl.com_data.i_temp[2] == 0)
    {
        {
            x = 72;
            y = 199;
            int const num_width = 10;
            int const num_interval = 3;
            int const num_height = 22;
            int x1 = x;
            int y1 = y + num_height / 2 - 2;
            int x2 = x1 + num_width - 1;
            int y2 = y1 + 3;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
            x1 += num_width + num_interval;
            x2 = x1 + num_width - 1;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
        }
        {
            x = 158;
            y = 199;
            int const num_width = 10;
            int const num_interval = 3;
            int const num_height = 22;
            int x1 = x;
            int y1 = y + num_height / 2 - 2;
            int x2 = x1 + num_width - 1;
            int y2 = y1 + 3;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
            x1 += num_width + num_interval;
            x2 = x1 + num_width - 1;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
        }
        {
            x = 92;
            y = 18;
            int const num_width = 10;
            int const num_interval = 3;
            int const num_height = 22;
            int x1 = x;
            int y1 = y + num_height / 2 - 2;
            int x2 = x1 + num_width - 1;
            int y2 = y1 + 3;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
            x1 += num_width + num_interval;
            x2 = x1 + num_width - 1;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
        }
    }
    else
    {
        disp_draw_user_image_num(72, 199, disp_ctrl.com_data.i_temp[2], 13, img_j301_TAB);
        disp_draw_user_image_num(158, 199, disp_ctrl.com_data.i_temp[1], 13, img_j301_TAB);
        disp_draw_user_image_num_with_digits(92, 18, disp_ctrl.com_data.i_temp[0], 3, 15, img_j301_TAB);
    }

    disp_draw_user_image(137, 18 + 7, img_common_bpm);

    int const start_x = 36;
    for (int i = 0; i < disp_ctrl.com_data.i_temp[3] - 1; i++)
    {
        if (detail_buffer[i] != 0 && detail_buffer[i + 1] != 0)
        {
            disp_draw_hr_line(start_x + i * 2, detail_buffer[i], start_x + (i + 1) * 2, detail_buffer[i + 1]);
        }
    }
}
static void show_time(int hour, int min)
{
    disp_draw_user_image(207, 3, img_j101_TAB[hour / 10]);
    disp_draw_user_image(214, 3, img_j101_TAB[hour % 10]);
    disp_draw_user_image(224, 3, img_j101_TAB[min / 10]);
    disp_draw_user_image(231, 3, img_j101_TAB[min % 10]);
    disp_draw_user_image(221, 3, img_j101_two_point);
}
void view_sports_state_0_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[0] = sports_mode_get_now_mode();
        disp_ctrl.com_data.i_temp[2] = sports_mode_get_sports_duration(); //时间
        if (hr_if_band_wear())
        {
            disp_ctrl.com_data.i_temp[9] = hr_get_hr_data();
        }
        else
        {
            disp_ctrl.com_data.i_temp[9] = 0;
        }
        disp_ctrl.com_data.i_temp[5] = sports_mode_get_sports_calorie() / 1000;
        if (system_config_get_km_mi() != 0)
        {
            disp_ctrl.com_data.i_temp[6] = sports_mode_get_sports_mileage() * 0.6213712;
            disp_ctrl.com_data.i_temp[7] = 0;
        }
        else
        {
            disp_ctrl.com_data.i_temp[6] = sports_mode_get_sports_mileage();
            disp_ctrl.com_data.i_temp[7] = 1;
        }
        if (disp_ctrl.auto_refresh_count == 0)
        {
            if (disp_ctrl.com_data.i_temp[2] < 0) //首次显示时间长一些
            {
                disp_ctrl.auto_close_delay = 10000;
            }
        }
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.com_data.i_temp[1] = hr_if_band_wear();
    }
    if (disp_ctrl.com_data.i_temp[2] <= 0)
    {
        disp_ctrl.auto_refresh_time = 100;
        switch (disp_ctrl.com_data.i_temp[2])
        {
        case -3:
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 39, 51, FILE_IMG_S301_start_3_39_51);
            break;
        case -2:
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 39, 51, FILE_IMG_S302_start_2_39_51);
            break;
        case -1:
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 39, 51, FILE_IMG_S303_start_1_39_51);
            disp_ctrl.com_data.i_temp[8] = 1;
            break;
        case 0:
            if (disp_ctrl.com_data.i_temp[8])
            {
                disp_ctrl.com_data.i_temp[8] = 0;
                motor_start(200);
                disp_ctrl.auto_close_delay = DISP_CLOSE_TIME_DEFAULT;
            }
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 39, 51, FILE_IMG_S304_start_go_39_51);
            break;
        }
        return;
    }
    if (!disp_ctrl.com_data.i_temp[1])
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 25, 73, FILE_IMG_J115_notwearingawatch_25_73);
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 18, 183, FILE_IMG_J115_notwearingawatch_text_18_183);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_AlertNotWear, FILE_IMG_localize_en_AlertNotWear_18_183, 18, 183, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_AlertNotWear, 50);
        return;
    }
    show_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    show_sport_mode_icon(disp_ctrl.com_data.i_temp[0]);
    show_sport_mode_text(disp_ctrl.com_data.i_temp[0]);
    {
        int duration = disp_ctrl.com_data.i_temp[2];
        int second = duration % 60;
        duration /= 60;
        int minute = duration % 60;
        duration /= 60;
        int hour = duration % 60;
        int x = 62;
        int const y = 69;
        int const font_width = 16;
        int const point_width = 7;
        int const interval = 3;
        disp_draw_user_image(x, y, img_j501_TAB[hour / 10]);
        x += font_width + interval;
        disp_draw_user_image(x, y, img_j501_TAB[hour % 10]);
        x += font_width + interval;
        disp_draw_user_image(x, y, img_j501_two_point);
        x += point_width + interval;
        disp_draw_user_image(x, y, img_j501_TAB[minute / 10]);
        x += font_width + interval;
        disp_draw_user_image(x, y, img_j501_TAB[minute % 10]);
        x += font_width + interval;
        disp_draw_user_image(x, y, img_j501_two_point);
        x += point_width + interval;
        disp_draw_user_image(x, y, img_j501_TAB[second / 10]);
        x += font_width + interval;
        disp_draw_user_image(x, y, img_j501_TAB[second % 10]);
    }
    {
        int const calorie_distance_y = 137 + 2;
        int mode = disp_ctrl.com_data.i_temp[0];
        int kcal = disp_ctrl.com_data.i_temp[5];
        int mileage = disp_ctrl.com_data.i_temp[6];
        switch (mode)
        {
        case sports_mode_mode_ride:
        case sports_mode_mode_work_out:
        case sports_mode_mode_swing:
        {
            int x_end = disp_draw_user_image_num(62, calorie_distance_y, kcal, 14 + 3, img_j401_TAB);
            disp_draw_user_image(x_end, calorie_distance_y + 12, img_common_kcal);
            break;
        }

        case sports_mode_mode_run_2:
        case sports_mode_mode_walk:
        {
            int x = disp_draw_user_image_num(62, calorie_distance_y, mileage / 1000, 14 + 3, img_j401_TAB);
            disp_draw_user_image(x, calorie_distance_y, img_j401_point);
            x += 5 + 3;
            x = disp_draw_user_image_num_with_digits(x, calorie_distance_y, mileage % 1000 / 10, 2, 14 + 3, img_j401_TAB);
            if (disp_ctrl.com_data.i_temp[7])
            {
                disp_draw_user_image(x + 4, calorie_distance_y + 15, img_common_km);
            }
            else
            {
                disp_draw_user_image(x + 4, calorie_distance_y + 15, img_common_mi);
            }
            break;
        }
        }
    }
    {
        int heart_rate = disp_ctrl.com_data.i_temp[9];
        int duration = disp_ctrl.com_data.i_temp[2];
        if (heart_rate > 220)
        {
            heart_rate = 220;
        }
        if (heart_rate < 40 || duration < 10)
        {
            if (disp_ctrl.auto_refresh_count % 2 == 0)
            {
                disp_draw_rect_color(86 - 3 - 14, 208 - 2, 86 - 3, 208 + 1, RGB565_COLOR_WHITE);
                disp_draw_rect_color(86 + 3, 208 - 2, 86 + 3 + 14, 208 + 1, RGB565_COLOR_WHITE);
            }
        }
        else
        {
            disp_draw_user_image(62, 193, img_j401_TAB[heart_rate / 100]);
            heart_rate %= 100;
            disp_draw_user_image(62 + 17, 193, img_j401_TAB[heart_rate / 10]);
            heart_rate %= 10;
            disp_draw_user_image(62 + 17 + 17, 193, img_j401_TAB[heart_rate]);
        }
        disp_draw_user_image(113, 207, img_common_bpm);
    }
}
void view_sports_display(int view)
{
    if (is_level_1_view(view))
    {
        view_sports_level_1_display(view);
    }
    else
    {
        switch (view)
        {
        case view_sports_base:
            view_sports_base_display();
            break;
        case view_sports_level_1_back:
            view_sports_level_1_back_display();
            break;
        case view_sports_state_0:
            view_sports_state_0_display();
            break;
        case view_sports_result_0:
            view_sports_result_0_display();
            break;
        case view_sports_result_1:
            view_sports_result_1_display();
            break;
        case view_sports_error_short:
            view_sports_error_short_display();
            break;
        case view_sports_prompt_end:
            view_sports_prompt_end_display();
        default:
            break;
        }
    }
}
view_event_result_t view_sports_level_1_back_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
        display_show(view_sports_level_1_begin, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        display_show(view_sports, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_state_0_event(event_param_t const *param)
{
    int duration = sports_mode_get_sports_duration();
    if (duration <= 0)
    {
        switch (param->event)
        {
#ifdef EVENT_PREVIOUS_VIEW
        case event_move_previous_view:
#endif
#ifdef EVENT_LEVEL_UP
        case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
        case event_move_level_down:
#endif
#ifdef EVENT_NEXT_VIEW
        case event_move_next_view:
#endif
#ifdef EVENT_BUTTON_FRONT
        case event_button_front_press:
        case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
        case BUTTON_EVENT_TP_TAP:
        case BUTTON_EVENT_TP_TAP_LONG:
#endif
            return view_event_result_handled;
        }
    }
    switch (param->event)
    {
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
        sports_mode_stop();
        return view_event_result_handled;

#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_error_short_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
    case event_button_front_long_press:
#endif
        display_show(view_sports, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_prompt_end_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
    case event_button_front_long_press:
#endif
        display_show(view_sports_result_0, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_result_0_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
    {
        return view_event_result_handled;
    }
#endif //!< #ifdef EVENT_PREVIOUS_VIEW
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
    {
        int new_view = view_sports_result_1;
        display_show(new_view, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
    }
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
        display_show(view_sports, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_result_1_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
        return view_event_result_handled;
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
        display_show(view_sports_result_0, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
        display_show(view_sports, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}

view_event_result_t view_sports_event(int view, event_param_t const *param)
{
    if (is_level_1_view(view))
    {
        return view_sports_level_1_event(view, param);
    }
    else
    {
        switch (view)
        {
        case view_sports_level_1_back:
            return view_sports_level_1_back_event(param);
        case view_sports_base:
            switch (param->event)
            {
#ifdef EVENT_BUTTON_FRONT
            case event_button_front_long_press:
#endif
#ifdef EVENT_LEVEL_DOWN
            case event_move_level_down:
#endif
                display_show(view_sports_level_1_begin, DISP_MODE_DOWN_MOVE);
                return view_event_result_handled;
            }
            break;
        case view_sports_state_0:
            return view_sports_state_0_event(param);
        case view_sports_error_short:
            return view_sports_error_short_event(param);
        case view_sports_prompt_end:
            return view_sports_prompt_end_event(param);
        case view_sports_result_0:
            return view_sports_result_0_event(param);
        case view_sports_result_1:
            return view_sports_result_1_event(param);
        default:
            break;
        }
    }
    return view_event_result_not_handled;
}
